const http = require('http')
const config=require('../config')
const math=require('mathjs')
const qs=require('querystring')

const Coinall = async () => { //获取所有币种
    const result = new Promise((resolve, reject) => {
        let opt={
            hostname:config.hostname,
            port: config.port,
            path:'/coin/all',
            method: "GET"
        }
        let req=http.request(opt,(res) => {
            res.setEncoding('utf8');
            let data = ''
            res.on('data', (chunk) => {
                data += chunk
            });
            res.on('end', () => {
                resolve(JSON.parse(data).data)
            })
        })
        req.on('error', (e) => {
            reject(e)
        })
        //写入数据到请求主体
        req.end()
    })
    return result
}

const allMine = async (ctx) => { //请求用户余额
    let coinallarr=await Coinall()
    const userid =  ctx.session.userId
    if(userid){
        const result = new Promise( (resolve,reject) => {
            let opt={
                hostname:config.hostname,
                port: config.port,
                path:`/coin/mine/${userid}`,
                method: "GET"
            }
            let req=http.request(opt,(res) => {
                res.setEncoding('utf8')
                let resdata=''
                res.on('data',(data) => {
                    resdata+=data
                })
                res.on('end', () => {
                    coinallarr.forEach((ele,index) => {
                        if(JSON.parse(resdata).data.length){
                            JSON.parse(resdata).data.forEach((ele2,index2) => {
                                if(ele.coin==='HPS'){
                                    if(ele.lastFaucetTime && ele.lastFaucetTime!=null){
                                        ctx.session.lastFaucetTime=ele.lastFaucetTime
                                    }
                                }
                                if(ele.coin===ele2.coin){
                                    ele=Object.assign(ele,ele2)
                                }
                                if(!String(ele.balance)){
                                    ele.balance='0.00'
                                }
                            })
                        }else{
                            ele.balance='0.00'
                        }
                        if(isNaN(Number(ele.balance))){
                            ele.balance='0.00'
                        }else{
                            ele.balance=math.format(parseFloat(ele.balance), { notation: "fixed", precision: 8 })
                        }
                    })
                    resolve(coinallarr)
                })
            })
            req.on('error',(e) => {
                reject(e)
            })
            req.end()
        })
        return result
    }else{
        return []
    }
}

const coinPostHistory = async (ctx,next) => { //查询用户资金历史
    let {coin,type,p,s}=ctx.request.body
    const userId =  ctx.session.userId
    if(userId){
        let url=''
        if(type&&coin){
            url=`/coin/history/${userId}?coin=${coin}&type=${type}&p=${p}&s=${s}`
        }else if(type&&coin==''){
            url=`/coin/history/${userId}?type=${type}&p=${p}&s=${s}`
        }else if(type==''&&coin){
            url=`/coin/history/${userId}?coin=${coin}&p=${p}&s=${s}`
        }else if(type==''&&coin==''){
            url=`/coin/history/${userId}?p=${p}&s=${s}`
        }
        const result = new Promise((resolve,reject) => {
            let opt={
                hostname: config.hostname,
                port: config.port,
                path: url,
                method: "GET"
            }
            let req=http.request(opt,(res) => {
                res.setEncoding('utf8')
                let resdata=''
                res.on('data',(chunk) => {
                    resdata+=chunk
                })
                res.on('end',() => {
                    let data=JSON.parse(resdata)
                    data.data.rate=data.rate
                    resolve(data)
                })
            })
            req.on('error',(e) => {
                reject(e)
            })
            req.end()
        })
        let data=await result
        if(data.code){
            ctx.error(data)
        }else{
            ctx.success(data)
        }
    }
}

module.exports={
    Coinall,
    allMine,
    coinPostHistory
}